package br.com.fafica.sisape.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import br.com.fafica.sisape.conexao.ConnectionFactory;
import br.com.fafica.sisape.model.Medico;

public class DAOMedico implements InterfaceDAOMedico{

	public void createMedico(Medico medico, Integer cod) throws SQLException {

		Connection con = new ConnectionFactory().getConnection();

		String sql = "insert into Medico " + "(CodUsuario, Nome, Telefone, Email, Crm) "
				+ "values (?,?,?,?,?)";
		try {

			PreparedStatement stmp = (PreparedStatement) con
					.prepareStatement(sql);
			stmp.setInt(1, cod);
			stmp.setString(2, medico.getNome());
			stmp.setString(3, medico.getTelefone());
			stmp.setString(4, medico.getEmail());
			stmp.setString(5, medico.getCrm());
			stmp.execute();
			stmp.close();
			System.out.println("Salvo com Sucesso");
		} catch (SQLException e) {
			System.out.println(e.getMessage());
			e.printStackTrace();
		} finally {
			con.close();
		}

	}

	public ArrayList<Medico> readMedico() throws SQLException {
		Connection con = new ConnectionFactory().getConnection();

		String sql = "Select * from Medico";

		try {
			PreparedStatement stmt = con.prepareStatement(sql);
			ResultSet rs = stmt.executeQuery();

			ArrayList<Medico> lista = new ArrayList<Medico>();
			while (rs.next()) {
				Medico medico = new Medico();
				medico.setNome(rs.getString("Nome"));
				medico.setTelefone(rs.getString("Telefone"));
				medico.setEmail(rs.getString("Email"));
				medico.setCrm(rs.getString("Crm"));
				medico.setCod(rs.getInt("Cod"));
				lista.add(medico);
			}
			rs.close();
			stmt.close();
			return lista;
		} catch (SQLException e) {
			System.out.println(e.getMessage());
			e.printStackTrace();
		} finally {
			con.close();
		}
		return null;
	}

	public void updateMedico(Medico medico) throws SQLException {

		Connection con = new ConnectionFactory().getConnection();

		String sql = "update Medico set Nome=?" + " ,Telefone=?"
				+ " ,Email=?"+" ,Crm=?" + " where Cod=?";
		try {

			PreparedStatement stmp = (PreparedStatement) con
					.prepareStatement(sql);
			stmp.setString(1, medico.getNome());
			stmp.setString(2, medico.getTelefone());
			stmp.setString(3, medico.getEmail());
			stmp.setString(4, medico.getCrm());
			stmp.setInt(5, medico.getCod());
			stmp.execute();
			stmp.close();
			System.out.println("Salvo com Sucesso");
		} catch (SQLException e) {
			System.out.println(e.getMessage());
			e.printStackTrace();
		} finally {
			con.close();
		}
	}

	public void deleteMedico(Medico medico) throws SQLException {

		Connection con = new ConnectionFactory().getConnection();

		String sql = "delete from Medico where Cod=?";
		try {

			PreparedStatement stmp = (PreparedStatement) con
					.prepareStatement(sql);
			stmp.setInt(1, medico.getCod());
			stmp.execute();
			stmp.close();
			System.out.println("Deletado com Sucesso");
		} catch (SQLException e) {
			System.out.println(e.getMessage());
			e.printStackTrace();
		} finally {
			con.close();
		}

	}
}